package com.example.smartstart.service.impl;

import com.example.smartstart.entity.Point;
import com.example.smartstart.mapper.PointMapper;
import com.example.smartstart.service.IPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dlx
 * @since 2024-07-22
 */
@Service
public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements IPointService {

    @Autowired
    private PointMapper pointMapper;

    @Override
    public Integer awardOrSpendPoint(Point point){
        Point userPoint = pointMapper.selectById(point.getUserId());
        Integer havePoints = userPoint.getPointCount();
        Integer awardOrSpendPoints = point.getPointCount();
        if (havePoints >= -awardOrSpendPoints){
            point.setPointCount(havePoints+awardOrSpendPoints);
            pointMapper.updateById(point);
            return havePoints + awardOrSpendPoints;
        }else {
            return -1;
        }
    }

}
